﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using Phones.Client.Model;
using Phones.Client.Repository;

namespace Phones.Client.Repository
{
    public class DepartmentsRepository : BaseRepository<Department>, IDisposable
    {
        public DepartmentsRepository(PhonesEntities context) : base(context) { }
        public DepartmentsRepository() : base() { }

        public List<Department> GetList(ServiceNode node, Organisation org)
        {

            if (node == null)  return null;
            if (org == null) return null;
            
            var queryorg = from d in Context.Departments
                           .Include("Contacts")
                            join c in Context.Contacts on d.ID equals c.Department.ID
                            join o in Context.Organisations on d.Organisation.ID equals o.ID
                            where c.City.ServiceNode.ID == node.ID && o.ID == org.ID
                            orderby d.Name ascending
                            select d;

            var list = queryorg.Distinct().ToList();
            //list.ForEach((x) => Context.Detach(x));
            return list;

        }
    }
}
